package com.jingyong.infosite.service;

import com.jingyong.infosite.dto.request.NewsAssortListRequest;
import com.jingyong.infosite.dto.request.NewsDetailRequest;
import com.jingyong.infosite.dto.response.NewsAssortListResponse;
import com.jingyong.infosite.dto.response.NewsDetail;
import com.jingyong.infosite.dto.response.NewsImportantResponse;
import com.mongodb.BasicDBList;
import com.mongodb.client.FindIterable;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;

import java.util.LinkedList;
import java.util.List;

@Service
public class InfosService {
	@Autowired
	private MongoTemplate mongoTemplate;

	public Object query() {
		MongoCollection<Document> col = mongoTemplate.getCollection("news");
		BasicDBObject filter = new BasicDBObject();
		return col.find(filter);
	}

	public Object queryDeals(String years) {
		MongoCollection<Document> col = mongoTemplate.getCollection("platform_deal_datas");
		BasicDBObject filter = new BasicDBObject();
		filter.append("data.years", years);
		return col.find(filter);
	}

	public NewsDetail getNewsDetail(NewsDetailRequest req) {
		MongoCollection<Document> col = mongoTemplate.getCollection("news");
		BasicDBObject condition = new BasicDBObject();
		BasicDBList condList = new BasicDBList();
		BasicDBObject filter = new BasicDBObject();
		filter.put("_id",req.getNews_id());
		filter.put("data_type",req.getAssort());
		condList.add(filter);
		condition.put("$and", condList);
		Document doc = col.find(filter).first();
		NewsDetail newsDetail = new NewsDetail();
		if (doc != null) {
			newsDetail.setId(Long.parseLong(String.valueOf(doc.get("_id"))));
			newsDetail.setTitle(String.valueOf(doc.get("title")));
			newsDetail.setAuthor(String.valueOf(doc.get("author")));
			newsDetail.setSource(String.valueOf(doc.get("source")));
			newsDetail.setPub_time(String.valueOf(doc.get("pub_time")));
			newsDetail.setSummary(String.valueOf(doc.get("summary")));
			newsDetail.setImg_url(String.valueOf(doc.get("img_url")));
			newsDetail.setContent(String.valueOf(doc.get("content")));
		}
		return newsDetail;
	}

	public NewsImportantResponse getNewsImportant() {
		MongoCollection<Document> col = mongoTemplate.getCollection("news");
		BasicDBObject filter = new BasicDBObject();
		filter.append("data_type", "main");
		FindIterable<Document> docs = col.find(filter).sort(new BasicDBObject("_id", -1)).limit(6);
		NewsImportantResponse response = new NewsImportantResponse();
		List<NewsDetail> items = new LinkedList<>();
		int len = 0;
		for (Document doc : docs) {
			NewsDetail newsDetail = new NewsDetail();
			newsDetail.setId(Long.parseLong(String.valueOf(doc.get("_id"))));
			newsDetail.setTitle(String.valueOf(doc.get("title")));
			newsDetail.setAuthor(String.valueOf(doc.get("author")));
			newsDetail.setSource(String.valueOf(doc.get("source")));
			newsDetail.setPub_time(String.valueOf(doc.get("pub_time")));
			newsDetail.setSummary(String.valueOf(doc.get("summary")));
			newsDetail.setImg_url(String.valueOf(doc.get("img_url")));
			newsDetail.setContent(String.valueOf(doc.get("content")));
			items.add(newsDetail);
			len++;
		}
		response.setLen(len);
		response.setItems(items);
		return response;
	}

	public NewsAssortListResponse getNewsAssortList(NewsAssortListRequest req) {
		MongoCollection<Document> col = mongoTemplate.getCollection("news");
		BasicDBObject filter = new BasicDBObject();
		filter.append("data_type", req.getAssort());
		FindIterable<Document> docs = col.find(filter).sort(new BasicDBObject("_id", -1));
		NewsAssortListResponse response = new NewsAssortListResponse();
		List<NewsDetail> items = new LinkedList<>();
		int len = req.getLen() > 0 ? req.getLen() : 20;
		int page = req.getPage() > 0 ? req.getPage() : 1;
		int total = 0;
		int i = 0;
		int start = len * (page - 1);
		int end = start + len;
		for (Document doc : docs) {
			total++;
			if (start <= total && total <= end) {
				NewsDetail newsDetail = new NewsDetail();
				newsDetail.setId(Long.parseLong(String.valueOf(doc.get("_id"))));
				newsDetail.setTitle(String.valueOf(doc.get("title")));
				newsDetail.setAuthor(String.valueOf(doc.get("author")));
				newsDetail.setSource(String.valueOf(doc.get("source")));
				newsDetail.setPub_time(String.valueOf(doc.get("pub_time")));
				newsDetail.setSummary(String.valueOf(doc.get("summary")));
				newsDetail.setImg_url(String.valueOf(doc.get("img_url")));
				newsDetail.setContent(String.valueOf(doc.get("content")));
				items.add(newsDetail);
				i++;
			}
		}
		int total_page = total / len;
		if (total > 0 && total % len > 0) {
			total_page++;
		}
		response.setLen(i);
		response.setPage(page);
		response.setTotal(total);
		response.setTotal_page(total_page);
		response.setAssort(req.getAssort());
		response.setItems(items);
		return response;
	}

	public NewsAssortListResponse getNewsAssortListNoContent(NewsAssortListRequest req) {
		MongoCollection<Document> col = mongoTemplate.getCollection("news");
		BasicDBObject filter = new BasicDBObject();
		filter.append("data_type", req.getAssort());
		FindIterable<Document> docs = col.find(filter).sort(new BasicDBObject("_id", -1));
		NewsAssortListResponse response = new NewsAssortListResponse();
		List<NewsDetail> items = new LinkedList<>();
		int len = req.getLen() > 0 ? req.getLen() : 20;
		int page = req.getPage() > 0 ? req.getPage() : 1;
		int total = 0;
		int i = 0;
		int start = len * (page - 1);
		int end = start + len;
		for (Document doc : docs) {
			total++;
			if (start <= total && total <= end) {
				NewsDetail newsDetail = new NewsDetail();
				newsDetail.setId(Long.parseLong(String.valueOf(doc.get("_id"))));
				newsDetail.setTitle(String.valueOf(doc.get("title")));
				newsDetail.setAuthor(String.valueOf(doc.get("author")));
				newsDetail.setSource(String.valueOf(doc.get("source")));
				newsDetail.setPub_time(String.valueOf(doc.get("pub_time")));
				newsDetail.setSummary(String.valueOf(doc.get("summary")));
				newsDetail.setImg_url(String.valueOf(doc.get("img_url")));
				newsDetail.setContent("");
				items.add(newsDetail);
				i++;
			}
		}
		int total_page = total / len;
		if (total > 0 && total % len > 0) {
			total_page++;
		}
		response.setLen(i);
		response.setPage(page);
		response.setTotal(total);
		response.setTotal_page(total_page);
		response.setAssort(req.getAssort());
		response.setItems(items);
		return response;
	}
}
